/**
  ******************************************************************************
  * @file      startup_um321xf.s
  * @author    MCD Application Team
  * @brief     um321xf devices vector table GCC toolchain.
  *            This module performs:
  *                - Set the initial SP
  *                - Set the initial PC == Reset_Handler,
  *                - Set the vector table entries with the exceptions ISR address
  *                - Branches to main in the C library (which eventually
  *                  calls main()).
  *            After Reset the Cortex-M0+ processor is in Thread mode,
  *            priority is Privileged, and the Stack is set to Main.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2018-2021 zlj
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */

.syntax unified
.cpu cortex-m0plus
.fpu softvfp
.thumb

.global g_pfnVectors
.global Default_Handler

/* start address for the initialization values of the .data section.*/
/* defined in linker script */
.word _sidata
/* start address for the .data section. defined in linker script */
.word _sdata
/* end address for the .data section. defined in linker script */
.word _edata
/* start address for the .bss section. defined in linker script */
.word _sbss
/* end address for the .bss section. defined in linker script */
.word _ebss

/**
 * @brief  This is the code that gets called when the processor first
 *          starts execution following a reset event. Only the absolutely
 *          necessary set is performed, after which the application
 *          supplied main() routine is called.
 * @param  None
 * @retval None
*/

  .section .text.Reset_Handler
  .weak Reset_Handler
  .type Reset_Handler, %function
Reset_Handler:
  ldr   r0, =_estack
  mov   sp, r0          /* set stack pointer */

/* Call the clock system initialization function.*/
  bl  SystemInit

/* Copy the data segment initializers from flash to SRAM */
  ldr r0, =_sdata
  ldr r1, =_edata
  ldr r2, =_sidata
  movs r3, #0
  b LoopCopyDataInit

CopyDataInit:
  ldr r4, [r2, r3]
  str r4, [r0, r3]
  adds r3, r3, #4

LoopCopyDataInit:
  adds r4, r0, r3
  cmp r4, r1
  bcc CopyDataInit

/* Zero fill the bss segment. */
  ldr r2, =_sbss
  ldr r4, =_ebss
  movs r3, #0
  b LoopFillZerobss

FillZerobss:
  str  r3, [r2]
  adds r2, r2, #4

LoopFillZerobss:
  cmp r2, r4
  bcc FillZerobss

/* Call static constructors */
  bl __libc_init_array
/* Call the application s entry point.*/
  bl main

LoopForever:
  b LoopForever

.size Reset_Handler, .-Reset_Handler

/**
 * @brief  This is the code that gets called when the processor receives an
 *         unexpected interrupt.  This simply enters an infinite loop, preserving
 *         the system state for examination by a debugger.
 *
 * @param  None
 * @retval None
*/
  .section .text.Default_Handler,"ax",%progbits
Default_Handler:
Infinite_Loop:
  b Infinite_Loop
  .size Default_Handler, .-Default_Handler

/******************************************************************************
*
* The minimal vector table for a Cortex M0.  Note that the proper constructs
* must be placed on this to ensure that it ends up at physical address
* 0x0000.0000.
*
******************************************************************************/
  .section .isr_vector,"a",%progbits
  .type g_pfnVectors, %object

g_pfnVectors:
  .long    _estack
  .long    Reset_Handler                      /*     Reset Handler */
  .long    NMI_Handler                        /* -14 NMI Handler */
  .long    HardFault_Handler                  /* -13 Hard Fault Handler */
  .long    0                                  /*     Reserved */
  .long    0                                  /*     Reserved */
  .long    0                                  /*     Reserved */
  .long    0                                  /*     Reserved */
  .long    0                                  /*     Reserved */
  .long    0                                  /*     Reserved */
  .long    0                                  /*     Reserved */
  .long    SVC_Handler                        /*  -5 SVCall Handler,SWI */
  .long    0                                  /*     Reserved */
  .long    0                                  /*     Reserved */
  .long    PendSV_Handler                     /*  -2 PendSV Handler */
  .long    SysTick_Handler                    /*  -1 SysTick Handler */

  /* Interrupts */
  .long    GPIO_PA_IRQHandler                  /*   0 Interrupt 0 */
  .long    GPIO_PB_IRQHandler                  /*   1 Interrupt 1 */
  .long    GPIO_PC_IRQHandler                  /*   2 Interrupt 2 */
  .long    GPIO_PD_IRQHandler                  /*   3 Interrupt 3 */
  .long    DMA_IRQHandler                    /*   4 Interrupt 4 */
  .long    LPTIMER23_IRQHandler              /*   5 Interrupt 5 */
  .long    UART0_IRQHandler                  /*   6 Interrupt 6 */
  .long    LPUART_IRQHandler                 /*   7 Interrupt 7 */
  .long    UART1_IRQHandler                  /*   8 Interrupt 8 */
  .long    I2C0_IRQHandler                    /*   9 Interrupt 9 */
  .long    SPI0_IRQHandler                   /*   10 Interrupt 10 */
  .long    SPI1_IRQHandler                   /*   11 Interrupt 11 */
  .long    CAN_IRQHandler                    /*   12 Interrupt 12 */
  .long    GPIO_PF_IRQHandler                   /*   13 Interrupt 13 */
  .long    GTIMER0_IRQHandler                /*   14 Interrupt 14 */
  .long    GTIMER1_IRQHandler                /*   15 Interrupt 15 */
  .long    GTIMER2_IRQHandler               /*   16 Interrupt 16 */
  .long    QSPI_IRQHandler                   /*   17 Interrupt 17 */
  .long    LPTIMER01_IRQHandler               /*   18 Interrupt 18 */
  .long    ANALOG_IRQHandler                  /*   19 Interrupt 19 */
  .long    GPIO_PE_IRQHandler                  /*   20 Interrupt 20 */
  .long    ATIMER_IRQHandler                  /*   21 Interrupt 21 */
  .long    WDT_IRQHandler                    /*   22 Interrupt 22 */
  .long    RTC_IRQHandler                    /*   23 Interrupt 23 */
  .long    ADC_IRQHandler                    /*   24 Interrupt 24 */
  .long    BTIMER01_IRQHandler               /*   25 Interrupt 25 */
  .long    I2C1_IRQHandler                  /*   26 Interrupt 26 */
  .long    WWDT_IRQHandler                   /*   27 Interrupt 27 */
  .long    UART2_IRQHandler                    /*   28 Interrupt 28 */
  .long    BTIMER23_IRQHandler                    /*   29 Interrupt 29 */
  .long    FLASH_IRQHandler                  /*   30 FLASH_IRQHandler 30 */
  .long    GPIO_PG_IRQHandler                 /*   31 FLASH_IRQHandler 30 */
  .space   ( 1* 4)                           /* Inte                rrupts 31 are left out */

  .size g_pfnVectors, .-g_pfnVectors

/*******************************************************************************
*
* Provide weak aliases for each Exception handler to the Default_Handler.
* As they are weak aliases, any function with the same name will override
* this definition.
*
*******************************************************************************/

  .weak      NMI_Handler
  .thumb_set NMI_Handler,Default_Handler

  .weak      HardFault_Handler
  .thumb_set HardFault_Handler,Default_Handler

  .weak      SVC_Handler
  .thumb_set SVC_Handler,Default_Handler

  .weak      PendSV_Handler
  .thumb_set PendSV_Handler,Default_Handler

  .weak      SysTick_Handler
  .thumb_set SysTick_Handler,Default_Handler

  .weak      GPIO_PA_IRQHandler
  .thumb_set GPIO_PA_IRQHandler,Default_Handler

  .weak      GPIO_PB_IRQHandler
  .thumb_set GPIO_PB_IRQHandler,Default_Handler

  .weak      GPIO_PC_IRQHandler
  .thumb_set GPIO_PC_IRQHandler,Default_Handler

  .weak      GPIO_PD_IRQHandler
  .thumb_set GPIO_PD_IRQHandler,Default_Handler

  .weak      DMA_IRQHandler
  .thumb_set DMA_IRQHandler,Default_Handler

  .weak      LPTIMER23_IRQHandler
  .thumb_set LPTIMER23_IRQHandler,Default_Handler

  .weak      UART0_IRQHandler
  .thumb_set UART0_IRQHandler,Default_Handler

  .weak      LPUART_IRQHandler
  .thumb_set LPUART_IRQHandler,Default_Handler

  .weak      UART1_IRQHandler
  .thumb_set UART1_IRQHandler,Default_Handler

  .weak      I2C_IRQHandler
  .thumb_set I2C_IRQHandler,Default_Handler

  .weak      SPI0_IRQHandler
  .thumb_set SPI0_IRQHandler,Default_Handler

  .weak      SPI1_IRQHandler
  .thumb_set SPI1_IRQHandler,Default_Handler

  .weak      CAN_IRQHandler
  .thumb_set CAN_IRQHandler,Default_Handler

  .weak      GPIO_PF_IRQHandler
  .thumb_set GPIO_PF_IRQHandler,Default_Handler

  .weak      GTIMER0_IRQHandler
  .thumb_set GTIMER0_IRQHandler,Default_Handler

  .weak      GTIMER1_IRQHandler
  .thumb_set GTIMER1_IRQHandler,Default_Handler

  .weak      GTIMER2_IRQHandler
  .thumb_set GTIMER2_IRQHandler,Default_Handler

  .weak      QSPI_IRQHandler
  .thumb_set QSPI_IRQHandler,Default_Handler

  .weak      LPTIMER01_IRQHandler
  .thumb_set LPTIMER01_IRQHandler,Default_Handler

  .weak      ANALOG_IRQHandler
  .thumb_set ANALOG_IRQHandler,Default_Handler

  .weak      GPIO_PE_IRQHandler
  .thumb_set GPIO_PE_IRQHandler,Default_Handler

  .weak      ATIMER_IRQHandler
  .thumb_set ATIMER_IRQHandler,Default_Handler

  .weak      LPTIMER_IRQHandler
  .thumb_set LPTIMER_IRQHandler,Default_Handler

  .weak      WDT_IRQHandler
  .thumb_set WDT_IRQHandler,Default_Handler

  .weak      RTC_IRQHandler
  .thumb_set RTC_IRQHandler,Default_Handler

  .weak      ADC_IRQHandler
  .thumb_set ADC_IRQHandler,Default_Handler

  .weak      BTIMER01_IRQHandler
  .thumb_set BTIMER01_IRQHandler,Default_Handler

  .weak      I2C0_IRQHandler
  .thumb_set I2C0_IRQHandler, Default_Handler

  .weak      I2C1_IRQHandler
  .thumb_set I2C1_IRQHandler, Default_Handler

  .weak      WWDT_IRQHandler
  .thumb_set WWDT_IRQHandler, Default_Handler

  .weak      UART2_IRQHandler
  .thumb_set UART2_IRQHandler, Default_Handler

  .weak      BTIMER23_IRQHandler
  .thumb_set BTIMER23_IRQHandler, Default_Handler

  .weak      FLASH_IRQHandler
  .thumb_set FLASH_IRQHandler,Default_Handler

  .weak      GPIO_PG_IRQHandler
  .thumb_set GPIO_PG_IRQHandler, Default_Handler
